<?php
/**
 * Class Crawl_weibo_info_from_wap_library
 * 通过 m.weibo.cn 获取微博信息
 */
class Crawl_weibo_info_from_wap_library extends Crawl_keyword_library{

    // 评论链接模板 uid: 博主id， mid 微博的数字id
    private $comm_url_temp ;

    // 评论列表模板
    private $single_comm_url_temp ;

    // 返回js的正则
    private $js_patt ;

    // cookie
    private $cookie_str ;

    // 微博树，包括微博信息、博主信息、转发列表等
    private $weibo_tree = array();

    function __construct(){
        parent::__construct();

        $this->_platform = 'sina';
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['sina_phone_val'];

        $this->comm_url_temp    = $this->_htmlsearch_config['comm_url_temp'];
        $this->single_comm_url_temp      = $this->_htmlsearch_config['single_comm_url_temp'];
        $this->js_patt   = $this->_htmlsearch_config['js_patt'];

        // 获取cookie时是否需要延迟5s？ 默认需要
        $need_delay = false;
        $this->cookie= $this->get_cookie($need_delay);
    }

    /**
     * 过滤和修改url为合适的形式
     * @param $urls
     * @return array
     */
    function filter_urls($urls){
        $clean_urls = array();

        // 如果是字符串的话
        if(is_string($urls)){
            $urls = explode("\n", $urls);
        }

        $urls = array_filter($urls);
        foreach($urls as $url){
            $clean_url = $this->filter_url($url);

            $clean_urls[] = array(
                'url' => $clean_url, // 抓取用的url
                's_url' => $url, // 本来的url
            );
        }
        return $clean_urls;
    }

    /**
     * 根据m.xxx 链接获取微博信息
     * @param $url
     */
    function fetch_weibo_comm($url)
    {
        $this->weibo_tree = array();
        try {
            if(empty($url)) return false;

            // 子数组
            $sibling_arr = array();
            $sibling_arr['url'] = $url;

            // 微博信息
            $weibo_info = $this->get_weibo_content($url);

            $sibling_arr['weibo_info'] = $weibo_info;

            $comment_url = str_replace(array('{uid}', '{mid}'), array($weibo_info->mblog->user->id, $weibo_info->mblog->id), $this->comm_url_temp);
            //pr($comment_url);

            $comm_content = $this->try_crawl($comment_url);
            //pr($comm_content);
            
            if(preg_match('|location.replace\("(.*?)"\)|', $comm_content, $matches)) {
            	$comm_content = $this->try_crawl($matches[1]);
            }
            
            // 评论信息
            $sibling_arr['comm'] = json_decode(str_replace("'", "\\\'", $comm_content));
            
            $file_name = "/home/www/crawl/application/logs/lizhifeng/dddd.txt" ;
            file_put_contents($file_name,$comm_content);

        } catch (Exception $e) {
            print_r($e);
            die;
        }

        $this->weibo_tree = $sibling_arr;

    }

    /**
     * 根据微博链接获取评论
     * @param $url  微博链接，是过滤后的 m.weibo.cn  打头的
     * @param $page  第几页的评论
     */
    function get_comms($para, $page){
        $this->weibo_tree = array();

        try {

            // 子数组
            $sibling_arr = array();
            $sibling_arr['page_size'] = 10;
            if($para['key'] == 'url'){
                $url = $para['val'];
                $sibling_arr['url'] = $url;
                $weibo_info = $this->get_weibo_content($url);
                $sibling_arr['comments_count'] = $weibo_info->mblog->comments_count;
                $sibling_arr['mid']  = $weibo_info->mblog->id;
            }elseif($para['key'] == 'mid'){
                $sibling_arr['mid']  = $para['val'];
            }

            $comment_url = str_replace(array('{page}', '{mid}'), array($page, $sibling_arr['mid']), $this->single_comm_url_temp);               
            $comm_content = $this->try_crawl($comment_url);
            // 评论信息
            $data = json_decode(str_replace("'", "\\\'", $comm_content));
            if($page == 1){
                $sibling_arr['comm'] = $data[1];
            }else{
                $sibling_arr['comm'] = $data[0];
            }

            $this->weibo_tree = $sibling_arr;

        } catch (Exception $e) {
            print_r($e);
            die;
        }
    }


    /**
     * 获取值
     * @return array
     */
    function get_weibo_tree(){
        return $this->weibo_tree;
    }

    /**
     * 格式化单个url
     * @param $url
     * @return mixed|string
     */
    function filter_url($url)
    {
        // 去除？后的字符
        if ($pos = strpos($url, '?')) {
            $clean_url = substr($url, 0, $pos);
        } else {
            $clean_url = $url;
        }

        // m.weibo.cn 可以直接访问
        $clean_url = trim(str_replace(array('http://www.weibo.com', 'weibo.com'), 'm.weibo.cn', $clean_url));
        return $clean_url;
    }

    /**
     * 获取微博数据 , 现在是从返回的页面的js代码里直接获取
     * @param $url
     * @param $matches
     * @return mixed
     */
    private function get_weibo_content($url)
    {
        $content = $this->try_crawl($url);
             
        if(preg_match('|location.replace\("(.*?)"\)|', $content, $matches)) {
        	$content = $this->try_crawl($matches[1]);
        }
        
        //file_put_contents("/Users/zengjie/web/demo.log", $content);
        //$file_name = "/home/www/crawl/application/logs/lizhifeng/dddd.txt" ;
        //file_put_contents($file_name,$content);
        
        preg_match($this->js_patt, $content, $matches);

        //如果解析不出内容，则冻结cookie
        if(empty($matches)){
        	//file_put_contents("/home/www/demo.log", $this->cookie['id'] ."#". $content . "\n", FILE_APPEND);
        	if( preg_match("/登录/", $content) || preg_match("|https://passport.weibo.cn/signin/login|", $content) || preg_match("/帐号异常/", $content) || preg_match("/<div class=\"u\"><div class=\"ut\">/", $content) ){
        	//if( preg_match("/登录/", $content) || preg_match("/帐号异常/", $content) || preg_match("/<div class=\"u\"><div class=\"ut\">/", $content) || preg_match("/loginWrapper/", $content) ){
        		if(!empty($this->cookie)){		
        			$this->label_cookie($this->cookie['id']);
        		}
        	}
        	
        }
        
        $weibo = json_decode(str_replace('\'', '"', $matches[1]));

        $weibo = $weibo->stage->single;

        $weibo_info = $weibo[1];
        return $weibo_info;
    }


    /**
     * 根据url获取微博信息
     * @param $url
     * @return bool
     */
    function fetch_weibo($url){
        $this->weibo_tree = array();
        try {
            if(empty($url)) return false;

            // 子数组
            $sibling_arr = array();
            $sibling_arr['url'] = $url;

            // 微博信息
            $weibo_info = $this->get_weibo_content($url);

            $sibling_arr['weibo_info'] = $weibo_info;

        } catch (Exception $e) {
            print_r($e);
            die;
        }

        $this->weibo_tree = $sibling_arr;
    }


    /**
     * 抓取url里的内容
     * @param $url
     * @return array|string
     */
    function try_crawl($url) {
        //private function try_crawl($url, $cookie, $retry = FALSE) {
        $cookie = $this->cookie;

        // 是否重试
        $retry = false;

        $parameters = $this->make_parameters($url, $cookie);

        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Sina Html Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Sina Html Un Use Retry And Un Use Proxy With Parameters: '.json_encode($parameters), FALSE, $this->_log_dir);
            return self::get_content($parameters);
        } else {
            return $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['sina_html'], FALSE);
        }

    }

    /**
     * 设置抓取条件
     * @param $url
     * @param string $cookie
     * @return mixed
     */
    private function make_parameters($url, $cookie = '') {
        //若传入cookie信息.则使用传入的cookie信息,用于调试cookie 是否有效使用 add by hejigui

        $parameters['url'] = $url;
        $parameters['method'] = self::METHOD_GET;

        if(!empty($cookie))
        {
            $parameters['cookie_str'] = true;
            $parameters['cookie'] = $cookie['cookie_str'];
        }
        $this->update_cookie_use_count($cookie['id']);

        return $this->_parameters = $parameters;
    }
}